import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/' , redirect: '/home'},
    { path: '/login', component: () => import('@/components/Login.vue')},
    { path: '/home', 
      component: () => import('@/components/Home.vue'),
      children: [
        { path: 'user', component: () => import('@/components/menus/User.vue') },
        { path: 'right', component: () => import('@/components/menus/Right.vue') },
        { path: 'good', component: () => import('@/components/menus/Good.vue') },
        { path: 'order', component: () => import('@/components/menus/Order.vue') },
        { path: 'setting', component: () => import('@/components/menus/Setting.vue') },
        { path: 'userDetail/:id', component: () => import('@/components/user/UserDetail.vue')},
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if(to.path === '/Login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if(token) {
      next()
    } else {
      next('/Login')
    }
  }
})
export default router